package com.game.tank.bean;

import com.game.tank.TankFrame;
import com.game.tank.enums.Dir;
import com.game.tank.enums.TankGroup;
import com.game.tank.loader.ResourceMgr;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

public class Tank {

    public static int WIDTH = ResourceMgr.tankU.getWidth();
    public static int HEIGHT = ResourceMgr.tankU.getHeight();

    private Random random = new Random();

    //坦克的初始位置
    private int x, y;

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    //速度
    private final int SPEED = 3;

    //初始方向
    private Dir dir = Dir.UP;

    private TankGroup group = TankGroup.BAD;

    //包含坦克爆炸
    private Explode explode = null;

    private Rectangle rect = new Rectangle();

    public TankGroup getGroup() {
        return group;
    }

    public void setGroup(TankGroup group) {
        this.group = group;
    }

    private boolean moving = true;
    private boolean live = true;

    private TankFrame tf = null;

    public Tank() {

    }

    public Tank(int x, int y, Dir dir,TankGroup group,TankFrame tf) {
        this.x =x;
        this.y = y;
        this.dir = dir;
        this.group = group;
        this.tf = tf;

        rect.x = x;
        rect.y=  y;
        rect.width = WIDTH;
        rect.height = HEIGHT;
    }


    //画出坦克，根据方向移动
    public void paint(Graphics graphics) {
        if (!isLive()) {
            boom(graphics);
        }
        Color c = graphics.getColor();
        BufferedImage tank = null;
        switch (dir) {
            case LEFT:
                tank = ResourceMgr.tankL;
                break;
            case RIGHT:
                tank = ResourceMgr.tankR;
                break;
            case UP:
                tank = ResourceMgr.tankU;
                break;
            case DOWN:
                tank = ResourceMgr.tankD;
                break;
            default:
                tank = ResourceMgr.tankU;
                break;
        }
        graphics.drawImage(tank, x, y, null);

        move();
    }

    private void boom(Graphics graphics) {
        explode = new Explode(x, y, tf);
        explode.paint(graphics);
        tf.tanks.remove(this);
    }

    private void move() {
        if (!moving) return;
            switch (dir) {
                case LEFT:
                    x -= SPEED;
                    break;
                case RIGHT:
                    x += SPEED;
                    break;
                case UP:
                    y -= SPEED;
                    break;
                case DOWN:
                    y += SPEED;
                    break;

            }
        if (this.group == TankGroup.BAD && random.nextInt(100) > 95) {
            this.fire();
        }
        if (this.group == TankGroup.BAD && random.nextInt(100) > 95) {
            randomDir();
        }

        boundsCheck();

        rect.x = x;
        rect.y = y;
    }

    private void boundsCheck() {
        boolean bounds = false;
        if (x < 2) {
            x = 2;
            bounds = true;
        }else if (y < 28) {
            y = 28;
            bounds = true;
        } else if (x > TankFrame.GAME_WIDTH - Tank.WIDTH -2) {
            x = TankFrame.GAME_WIDTH - WIDTH -2 ;
            bounds = true;
        } else if (y > TankFrame.GAME_HEIGHT - Tank.HEIGHT - 28) {
            y = TankFrame.GAME_HEIGHT - HEIGHT -28;
            bounds = true;
        }
        if (bounds && this.group == TankGroup.BAD) {
            //转换到任意没有障碍或边界的方向。
        }

    }

    private void randomDir() {
        this.dir = Dir.values()[random.nextInt(4)];
    }

    //打出一颗子弹。
    public void fire() {
        int bx = this.x + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
        int by = this.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;
        tf.bullets.add(new Bullet(bx, by, dir,getGroup(), tf));
    }

    public void die() {
        live = false;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public Dir getDir() {
        return dir;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    public boolean isMoving() {
        return moving;
    }

    public boolean isLive() {
        return live;
    }

    public void setLive(boolean live) {
        this.live = live;
    }

    public Rectangle getRect() {
        return rect;
    }
}
